package com.live.dengdengtv.activity.my;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.DianboActivity;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.RecordIdItem;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by leiyun on 2016/8/12.
 */
public class MyLiveActivity extends BaseActivity {
    private static final String TAG = "MyLiveActivity";
    private ListView my_live_lv;
    private MyLiveAdapter adapter;
    private List<RecordIdItem> listViewData = new ArrayList<RecordIdItem>();
    private TextView num_tv;
    private TextView hottest_tv;
    private TextView recent_tv;
    private boolean currIsTimeSort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_live);


        num_tv = (TextView)findViewById(R.id.num_tv);

        my_live_lv = (ListView)findViewById(R.id.my_live_lv);
        adapter = new MyLiveAdapter();
        my_live_lv.setAdapter(adapter);
        my_live_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, DianboActivity.class);
                intent.putExtra("vid", listViewData.get(position).vid);
                startActivity(intent);
            }
        });


        recent_tv = (TextView)findViewById(R.id.recent_tv);
        hottest_tv = (TextView)findViewById(R.id.hottest_tv);

        recent_tv.setOnClickListener(sortClickLis);
        hottest_tv.setOnClickListener(sortClickLis);

        currIsTimeSort = false;
        getRecordList(true);
    }


    private View.OnClickListener sortClickLis = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.e(TAG,v.getId()+" "+ currIsTimeSort);

            if(v == recent_tv && !currIsTimeSort){
                getRecordList(true);

                Drawable drawable = getResources().getDrawable(R.mipmap.rise, null);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                recent_tv.setCompoundDrawables(drawable,null,null,null);
                hottest_tv.setCompoundDrawables(null,null,null,null);
            } else if(v == hottest_tv && currIsTimeSort){
                getRecordList(false);

                Drawable drawable = getResources().getDrawable(R.mipmap.rise, null);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                recent_tv.setCompoundDrawables(null,null,null,null);
                hottest_tv.setCompoundDrawables(drawable,null,null,null);
            }
        }
    };

    private void getRecordList(boolean mIsTimeSort){
        HashMap<String, String> params = new HashMap<>();
        params.put("sort_type", mIsTimeSort?"1":"2");//返回排序类型 1--时间， 2--观看人数
        params.put("page","1");
        params.put("page_count","30");
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.get_recordList, params,1);
    }
    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                listViewData.clear();
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    RecordIdItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), RecordIdItem.class);
                    listViewData.add(liveItem);
                }
                adapter.notifyDataSetChanged();

                num_tv.setText(listViewData.size()+"");

                currIsTimeSort = !currIsTimeSort;
            }
        }
        return false;
    }


    private class MyLiveAdapter extends BaseAdapter{

        ViewHolder holder;
        @Override
        public int getCount() {
            return listViewData.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
                convertView = mInflater.inflate(R.layout.people_live_card,null);
                holder = new ViewHolder();
                holder.head_iv = (ImageView)convertView.findViewById(R.id.head_iv);

                holder.name_tv = (TextView)convertView.findViewById(R.id.name_tv);
                holder.playback_iv = (ImageView)convertView.findViewById(R.id.playback_iv);
                holder.sex_iv = (ImageView)convertView.findViewById(R.id.sex_iv);
                holder.level_iv = (ImageView)convertView.findViewById(R.id.level_iv);
                holder.date_tv = (TextView)convertView.findViewById(R.id.date_tv);
                holder.num_tv = (TextView)convertView.findViewById(R.id.num_tv);


                String userId = LoginInfo.getInstance().getUserId();
                String faceUrl = LoginInfo.getInstance().getAvatarAddress();
                String nickName = LoginInfo.getInstance().getUserNickname();
                String sex = LoginInfo.getInstance().getUserSex();

                if(!TextUtils.isEmpty(faceUrl)){
                    Glide.with(context).load(faceUrl).into(holder.head_iv);
                }
                if(!TextUtils.isEmpty(nickName)){
                    holder.name_tv.setText(nickName);
                }else {
                    holder.name_tv.setText(userId);
                }
                holder.name_tv.setText(nickName);
                if("1".equals(sex)) holder.sex_iv.setImageResource(R.mipmap.my_iconsex_man);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();

            }

            RecordIdItem recordIdItem = listViewData.get(position);
            holder.date_tv.setText(DateUtils.dateToString(new Date(recordIdItem.create_time*1000),"yyyy年M月d日"));
            holder.num_tv.setText(recordIdItem.watch_count+" 人");
            return convertView;
        }
    }

    private static class ViewHolder{
       public ImageView head_iv;
        public TextView name_tv;
        public ImageView playback_iv;
        public ImageView sex_iv;
        public ImageView level_iv;
        public TextView date_tv;
        public TextView num_tv;
    }
}
